package com.tianji.learning.mq;

import cn.hutool.core.util.ObjectUtil;
import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import static com.tianji.common.constants.MqConstants.Exchange.ORDER_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.ORDER_PAY_KEY;
import static com.tianji.common.constants.MqConstants.Queue.LEARNING_LESSON_PAY_QUEUE;

@Slf4j
@Component
@RequiredArgsConstructor
public class LessonChangeListener {

    private final ILearningLessonService lessonService;



    /**
     * 监听订单支付或课程报名的消息
     * @param
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = LEARNING_LESSON_PAY_QUEUE, durable = "true"),
            exchange = @Exchange(name = ORDER_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = ORDER_PAY_KEY
    ))
    public void listenOrderPayMsg(OrderBasicDTO orderMsg){
        // 1.健壮性判断
        if(ObjectUtil.isEmpty(orderMsg) || ObjectUtil.isEmpty(orderMsg.getUserId()) || ObjectUtil.isEmpty(orderMsg.getCourseIds())){
            return;
        }
        // 2.添加课程
        lessonService.addLessons(orderMsg.getUserId(), orderMsg.getCourseIds());
    }
}